package main

import (
	coolenvpb "coolcar/coolenv/api/gen/v1"
	"coolcar/coolenv/coolenv"
	"coolcar/coolenv/mq"
	"coolcar/shared/server"
	"log"
	"time"

	"github.com/namsral/flag"
	cmap "github.com/orcaman/concurrent-map"
	amqp "github.com/rabbitmq/amqp091-go"
	"go.uber.org/zap"
	"google.golang.org/grpc"
)

var addr = flag.String("addr", ":18001", "address to listen")
var amqpURL = flag.String("amqp_url", "amqp://root:123456@192.168.1.2:5672/", "amqp url")

func main() {
	flag.Parse()

	logger, err := server.NewZapLogger()
	if err != nil {
		log.Fatalf("cannot create logger: %v", err)
	}

	carPosChans := cmap.New() // 存储每辆车的模拟channel

	amqpConn, err := amqp.Dial(*amqpURL)
	if err != nil {
		logger.Fatal("cannot dial amqp", zap.Error(err))
	}
	defer amqpConn.Close()

	exchange := "pos_sim"
	pub, err := mq.NewPublisher(amqpConn, exchange)
	if err != nil {
		logger.Fatal("cannot create publisher", zap.Error(err))
	}

	posSimInterval := 1 * time.Second

	logger.Sugar().Fatal(server.RunGRPCServer(server.NewGRPCConfig(
		"coolenv", *addr, "",
		func(s *grpc.Server) {
			coolenvpb.RegisterAIServiceServer(s, coolenv.Service(
				carPosChans,
				pub,
				posSimInterval,
				logger,
			))
		},
		logger,
	)))
}
